package net.skyedu.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.skyedu.bean.FeeDetailByGrade;
import net.skyedu.bean.StuFeeSecond;
import net.skyedu.dao.AjaxDAO;

public class PaginationThird extends HttpServlet{
	private static final long serialVersionUID = 1L;

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
              doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		  response.setCharacterEncoding("utf-8");
		  	
		  	int currentpage = Integer.parseInt(request.getParameter("currentPage"));
	  		int pagesize = Integer.parseInt(request.getParameter("pageSize"));
//	  		int terId = Integer.parseInt(request.getParameter("terId"));
	  		int schId = Integer.parseInt(request.getParameter("schId"));
	  		int subId = Integer.parseInt(request.getParameter("subId"));
	  		int graId = Integer.parseInt(request.getParameter("graId"));
//	  		request.setAttribute("graid", graId);
//	  		int feeClass = Integer.parseInt(request.getParameter("feeClass"));
//	  		int surplusfee = Integer.parseInt(request.getParameter("surplusfee"));
	  		String teaName = request.getParameter("teaName");
//	  		String stuName = request.getParameter("stuName");
	  		String startTime = request.getParameter("startTime");
	  		String endTime = request.getParameter("endTime");
	  		StringBuffer sb = new StringBuffer();
	  		StringBuffer sb1 = new StringBuffer();
	  		if(startTime != null && !"".equals(startTime)){
	  			startTime = sb.append(startTime).append(" 00:00:00").toString();
	  		}
	  		if(endTime != null && !"".equals(endTime)){
	  			endTime = sb1.append(endTime).append(" 23:59:59").toString();
	  		}
			if(teaName != null && !"".equals(teaName)){
				teaName = URLDecoder.decode(teaName,"UTF-8");
			}
         
	  		int startIndex = 0;
	  		try{
        	  startIndex = (currentpage - 1) * pagesize;
        	  
        	  AjaxDAO ad = new AjaxDAO();
//		      StringBuffer jsonAttr = new StringBuffer("{\"pageCount\":\""+pageCount+"\",\"pageData\":[");
		      StringBuffer jsonAttr = new StringBuffer("[");
		      int pageCount = 0;
		      //学生查询
//        		  List<StuFeeSecond> stulist = ad.showlistSecond(terId, schId, subId, graId, teaName, stuName, startTime, endTime, startIndex, pagesize, 1,surplusfee);
//        		  pageCount = ad.showlistSecond(terId, schId, subId, graId, teaName, stuName, startTime, endTime, startIndex, pagesize, 0,surplusfee).size();
		      List<FeeDetailByGrade> list=ad.getDetailFeeInfo(schId, 0, subId,startTime,endTime);
		      pageCount=list.size();
		      double sumone=0.0d;
		      double sumtwo=0.0d;
		      double sumthree=0.0d;
		      double sumfour=0.0d;
		      double sumfive=0.0d;
		      double sumsix=0.0d;
		      double sumseven=0.0d;
		      double sumeight=0.0d;
		      double sumnine=0.0d;
		      double sumten=0.0d;
		      double sumeleven=0.0d;
		      double sumtwelve=0.0d;
		      double sumzh=0.0d;
        		  if(pageCount != 0){
        			  
        			    for(int i = 0; i < list.size(); i++){
        			    FeeDetailByGrade detail = list.get(i);
        			    int type=0;
			    		  if("英语".equals(detail.getSubjectName().trim()))
			    		  {
			    			  type=1;
			    		  }
			    		  else if("数学".equals(detail.getSubjectName().trim()))
			    		  {
			    			  type=2;
			    		  }
			    		  else if("语文".equals(detail.getSubjectName().trim()))
			    		  {
			    			  type=3;
			    		  }
			    		  else 
			    		  {
			    			  type=4;
			    		  }
			    		  jsonAttr.append("{");
			    		   sumone+=Double.parseDouble(detail.getOne());
					       sumtwo+=Double.parseDouble(detail.getTwo());
					       sumthree+=Double.parseDouble(detail.getThree());
					       sumfour+=Double.parseDouble(detail.getFour());
					       sumfive+=Double.parseDouble(detail.getFive());
					       sumsix+=Double.parseDouble(detail.getSix());
					       sumseven+=Double.parseDouble(detail.getSeven());
					       sumeight+=Double.parseDouble(detail.getEight());
					       sumnine+=Double.parseDouble(detail.getNine());
					       sumten+=Double.parseDouble(detail.getTen());
					       sumeleven+=Double.parseDouble(detail.getEleven());
					       sumtwelve+=Double.parseDouble(detail.getTwelve());  
					       sumzh+=Double.parseDouble(detail.getSum());
    			      jsonAttr.append("\"schoolname\":\""+detail.getSchoolName()+"\",");
    			      jsonAttr.append("\"subjectname\":\""+detail.getSubjectName()+"\",");
        			  jsonAttr.append("\"one\":\""+detail.getOne()+"\",");
        			  jsonAttr.append("\"two\":\""+detail.getTwo()+"\",");
        			  jsonAttr.append("\"three\":\""+detail.getThree()+"\",");
        			  jsonAttr.append("\"four\":\""+detail.getFour()+"\",");
        			  jsonAttr.append("\"five\":\""+detail.getFive()+"\",");
        			  jsonAttr.append("\"six\":\""+detail.getSix()+"\",");
        			  jsonAttr.append("\"seven\":\""+detail.getSeven()+"\",");
        			  jsonAttr.append("\"eight\":\""+detail.getEight()+"\",");
        			  jsonAttr.append("\"nine\":\""+detail.getNine()+"\",");
        			  jsonAttr.append("\"ten\":\""+detail.getTen()+"\",");
        			  jsonAttr.append("\"eleven\":\""+detail.getEleven()+"\",");
        			  jsonAttr.append("\"twelve\":\""+detail.getTwelve()+"\",");
        			  switch(graId)
        			  {
        			  case 0:
        				  jsonAttr.append("\"sum\":\""+detail.getSum()+"\",");
    			     	  break;
        			  case 1:
        			     	  jsonAttr.append("\"sum\":\""+detail.getOne()+"\",");
        			     	  break;
        			  case 2:
        				  jsonAttr.append("\"sum\":\""+detail.getTwo()+"\",");
    			     	  break;
        			  case 3:
        				  jsonAttr.append("\"sum\":\""+detail.getThree()+"\",");
    			     	  break;
        			  case 4:
        				  jsonAttr.append("\"sum\":\""+detail.getFour()+"\",");
    			     	  break;
        			  case 5:
        				  jsonAttr.append("\"sum\":\""+detail.getFive()+"\",");
    			     	  break;
        			  case 6:
        				  jsonAttr.append("\"sum\":\""+detail.getSix()+"\",");
    			     	  break;
        			  case 7:
        				  jsonAttr.append("\"sum\":\""+detail.getSeven()+"\",");
    			     	  break;
        			  case 8:
        				  jsonAttr.append("\"sum\":\""+detail.getEight()+"\",");
    			     	  break;
        			  case 9:
    			     	 jsonAttr.append("\"sum\":\""+detail.getOne()+"\",");
   			     	  	 break;
        			  case 10:
        				  jsonAttr.append("\"sum\":\""+detail.getTen()+"\",");
    			     	  break;
        			  case 11:
        				  jsonAttr.append("\"sum\":\""+detail.getEleven()+"\",");
    			     	  break;
        			  case 12:
        				  jsonAttr.append("\"sum\":\""+detail.getTwelve()+"\",");
    			     	  break;
    			      default:
    			     	  jsonAttr.append("\"sum\":\""+detail.getSum()+"\",");
       			     	  break;
        			  }
        			 
        			  jsonAttr.append("\"pageCount\":\""+pageCount+"\","); //记录总数
        			  jsonAttr.append("\"graid\":\""+graId+"\""); //记录总数
        			  
        			  jsonAttr.append("}");
//        			  if(i<list.size()-1){
//			    		  jsonAttr.append(",");
//			    	  }
        			  jsonAttr.append(",");
        			    }
        			    jsonAttr.append("{");
        			    jsonAttr.append("\"sumone\":\""+sumone+"\",");
        			    jsonAttr.append("\"sumtwo\":\""+sumtwo+"\",");
        			    jsonAttr.append("\"sumthree\":\""+sumthree+"\",");
        			    jsonAttr.append("\"sumfour\":\""+sumfour+"\",");
        			    jsonAttr.append("\"sumfive\":\""+sumfive+"\",");
        			    jsonAttr.append("\"sumsix\":\""+sumsix+"\",");
        			    jsonAttr.append("\"sumeight\":\""+sumeight+"\",");
        			    jsonAttr.append("\"sumnine\":\""+sumnine+"\",");
        			    jsonAttr.append("\"sumten\":\""+sumten+"\",");
        			    jsonAttr.append("\"sumeleven\":\""+sumeleven+"\",");
        			    jsonAttr.append("\"sumtwelve\":\""+sumtwelve+"\",");
        			    jsonAttr.append("\"sumzh\":\""+sumzh+"\"");
        			    jsonAttr.append("}");
        	  }else{
		    	  jsonAttr.append("{");
		    	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); 
		    	  jsonAttr.append("}");
		      }
		      
		      jsonAttr.append("]");
		      System.out.println("JSON:---"+jsonAttr);
		      PrintWriter out = response.getWriter();
		      out.print(jsonAttr);
		      out.flush();
		      out.close();
        	
          }catch(Exception e){
        	  //写到日志表中
        	  e.printStackTrace();
          }
       
		
		
		
		
	
	}
	
//	public StringBuffer getJsonAttr(int terId,StuFeeSecond stu,StringBuffer jsonAttr,Map<String,Object> tempmap)
//	{
//		 double sumShouldFee=0.0d;
//		  double sumActualFee=0.0d;
//		  double sumUnAcceptFee=0.0d;
//		  double sumsurplusFeeBefore=0.0d;
//		  double sumsurplusFee=0.0d;
//		  int num=stu.getAlreadyClaNum();
//		  int sumclaNum=0;
//		  int sumAlreadyClaNum=0;
//		  int sumSurplusClaNum=0;
//		  double sumalreadyfee=0.0d;
//		double shouldfee= stu.getShouldFee(stu.getClaNum());
//	    double feetemp=shouldfee-stu.getActualFee();
//	    double alreadyfee=stu.getAlreadyClaNum()*stu.getFeeperClass()*stu.getDiscount(); 
//	    sumalreadyfee+=alreadyfee;
//   	 sumsurplusFeeBefore+=stu.getBalance();;
//   	  sumShouldFee+=shouldfee;//应该交费
//   	  sumActualFee+=stu.getActualFee();//实际交费
//   	  sumUnAcceptFee+=stu.getUnAcceptFee();//未缴清
//   	  sumclaNum+=stu.getClaNum();//交费课次
//   	  sumAlreadyClaNum+=stu.getAlreadyClaNum();//已上课次
//   	  sumSurplusClaNum+=stu.getSurplusClaNum();//剩余课次
//   	tempmap.put("sumalreadyfee", sumalreadyfee);
//   	tempmap.put("sumShouldFee", sumShouldFee);
//   	tempmap.put("sumUnAcceptFee", sumUnAcceptFee);
//   	tempmap.put("sumclaNum", sumclaNum);
//   	tempmap.put("sumAlreadyClaNum", sumAlreadyClaNum);
//   	tempmap.put("sumSurplusClaNum",sumSurplusClaNum);
//   	  jsonAttr.append("{");
//   	  jsonAttr.append("\"stuId\":\""+stu.getStuId()+"\",");
//   	  jsonAttr.append("\"stuName\":\""+stu.getStuName()+"\",");
//   	  jsonAttr.append("\"terName\":\""+stu.getTerName()+"\",");
//   	  jsonAttr.append("\"schName\":\""+stu.getSchName()+"\",");
//   	  jsonAttr.append("\"subName\":\""+stu.getSubName()+"\",");
//   	  jsonAttr.append("\"graName\":\""+stu.getGraName()+"\",");
//   	  jsonAttr.append("\"shouldFee\":\""+stu.getShouldFee()+"\",");
//   	  jsonAttr.append("\"actualFee\":\""+stu.getActualFee()+"\",");
//   	  jsonAttr.append("\"discount\":\""+stu.getDiscount()+"\",");
//   	  jsonAttr.append("\"payTime\":\""+stu.getPayTime()+"\",");
//   	  jsonAttr.append("\"claNum\":\""+stu.getClaNum()+"\",");
//   	  jsonAttr.append("\"feeperClass\":\""+stu.getFeeperClass()+"\",");
//   	  jsonAttr.append("\"surplusbefore\":\""+stu.getSurplusbefore()+"\",");
//   	  
//   	  jsonAttr.append("\"alreadyFee\":\""+alreadyfee+"\",");
//   	  sumsurplusFee+=(stu.getActualFee()-alreadyfee);
//   	  jsonAttr.append("\"surplusFee\":\""+(stu.getActualFee()-alreadyfee)+"\",");
//   	  jsonAttr.append("\"unPayFee\":\""+(stu.getShouldFee()-stu.getActualFee())+"\",");
////   	  System.out.println(stu.getStuId()+"----"+stu.getClaId());
//   	  
//   	  
//   	  if(num != 0){
//   		  jsonAttr.append("\"yiNum\":\"<a href='javascript:void(0);' " +
//   		  "onclick='openWin("+terId+","+stu.getStuId()+");'><font color='red' style='font-weight:bold;'>"+num+"</font></a>\",");
//   	  }else{
//   		  jsonAttr.append("\"yiNum\":\"0\",");
//   	  }
//   	  int syNum = stu.getClaNum() - num;
//   	  jsonAttr.append("\"syNum\":\""+syNum+"\",");//剩余课次
//   	  jsonAttr.append("\"recordStaff\":\""+stu.getReceiver()+"\",");
//   	  
////   	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); //记录总数
////   	  jsonAttr.append("}");
//   	  
//		
//		return jsonAttr;
//	}
//	
	
	

	public void init() throws ServletException {}
}
